package com.ruoyi.kis.timeTask;

import com.alibaba.druid.support.json.JSONUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.kis.Vars.Constants;
import com.ruoyi.kis.domain.IcItems;
import com.ruoyi.kis.domain.OutSourceEntryF;
import com.ruoyi.kis.domain.PoInStock;
import com.ruoyi.kis.service.IKisService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Slf4j
@Component("kisM")
public class KisTask {

    @Autowired
    private IKisService kisService;

    private Logger logger = LoggerFactory.getLogger(KisTask.class);
    private String httpWmsItemsUrl = "";

    /**
     * 物料(主数据同步)
     * 第一次数据同步以及有物料新增或物料属性更改时下发给WMS
     */
    public void pushMaterial2Wms(){

        //获取当前同步时间
        Date fLastUpdateTime = kisService.queryCurrentTime(Constants.T_ICITEMS);
        //根据时间增量查询数据
        List<IcItems> icItemsList = kisService.queryItems(fLastUpdateTime);
        if(icItemsList != null && icItemsList.size() > 0){
            fLastUpdateTime = icItemsList.get(icItemsList.size() - 1).getFLastUpdateTime();
            Map<String,Object> params = new HashMap<>();
            params.put("tableName",Constants.OUT_SOURCE_IN_WARE_ORDER);
            params.put("fLastUpdateTime",fLastUpdateTime);
            kisService.updateCurrentTime(params);
            final String params2 = JSONUtils.toJSONString(icItemsList);//TODO json格式可能有问题，需要重新验
            final String s = HttpUtils.sendPost(httpWmsItemsUrl,params2);
            logger.info(s);
        }else{
            logger.info("无数据更新！");
        }


    }

    /**
     * 收料通知单下发
     *
     */

    public void pushPOInStock2Wms(){

//        Date poInStockTime = kisService.queryCurrentTime(Constants.POINSTOCK);
//        List<PoInStock> poInStockList = kisService.queryPoinStock(poInStockTime);
    }
    /**
     * 外购入库单
     *
     */
    public void outSourceInWareOrder(){
        Date orderTime = kisService.queryCurrentTime(Constants.OUT_SOURCE_IN_WARE_ORDER);
        List<OutSourceEntryF> outSourceEntryFList = kisService.queryOutSourceInWareOrder();
        Optional<OutSourceEntryF> maxRecord = outSourceEntryFList.stream()
                .max(Comparator.comparing(OutSourceEntryF::getFDate ));

        if (maxRecord.isPresent()) {
            OutSourceEntryF maxTimeRecord = maxRecord.get();
            final Date date = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,maxTimeRecord.getFDate() );
            Map<String,Object> params = new HashMap<>();
            params.put("tableName",Constants.OUT_SOURCE_IN_WARE_ORDER);
            params.put("fLastUpdateTime",date);
            kisService.updateCurrentTime(params);
            System.out.println("时间最大的记录是：" + maxTimeRecord.getFDate());
        } else {
            System.out.println("List为空，没有最大的记录");
        }
        final String params2 = JSONUtils.toJSONString(outSourceEntryFList);//TODO json格式可能有问题，需要重新验
        final String s = HttpUtils.sendPost(httpWmsItemsUrl,params2);
        logger.info(s);
        //return AjaxResult.success("OK",outSourceEntryFList);
    }

    /**
     * 生产入库
     */
    public void prodStor(){

    }
}
